package com.leador.utils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

/**
 * Created by liuha on 2016/9/11.
 */
public class Utils {

    public static boolean checkArg(String paramString1, String paramString2, StringBuffer paramStringBuffer) {
        StringBuffer localStringBuffer = new StringBuffer();
        String [] strings = splitStr(paramString1, ",");
        if (strings == null) {
//            entityCallBack(0, "参数错误:[" + paramString2 + "]");
            return false;
        }
        int i = strings.length;
        for (int j = 0; j < i; j++) {
            String str;
            String[] arrayOfString = splitStr(str = strings[j], "=");
            if ((arrayOfString != null) && (2 == arrayOfString.length)) {
                localStringBuffer.append("&").append(arrayOfString[0]).append("=").append(arrayOfString[1]);
            }
            else {
//                entityCallBack(0, "参数错误:[" + paramString2 + "]中的" + str);
                return false;
            }
        }
        paramStringBuffer.append(localStringBuffer);
        return true;
    }

    /**
     * 分割字符串
     * @param paramString1
     * @param paramString2
     * @return
     */
    public static String[] splitStr(String paramString1, String paramString2) {
        if (!TextUtils.isEmpty(paramString1)) {
            try {
                return paramString1.split(paramString2);
            }
            catch (Exception localException) {
                return null;
            }
        }
        return null;
    }



    /**
     * inputStram 转 String
     * @param inputStream
     * @return
     */
    public static String getString(InputStream inputStream) {
        return inputStreamToString(inputStream);
    }

    private static String inputStreamToString(InputStream inputStream) {
        if (inputStream == null) {
            return "";
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String str1 = "";
        String str3 = null;
        try {
            while (null != (str3 = reader.readLine())) {
                str1 = str1 + str3;
            }if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException localIOException1) {}finally {}
            }
            return str1;
        } catch (Exception localException) {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException localIOException2) {}finally {}
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                }
                catch (IOException localIOException3) {}finally {}
            }
        }
        return str1;
    }
    /**
     * 获取包名
     * @param context
     * @return
     */
    public static String getPackageName(Context context){
        try {
            return context.getPackageName();
        }
        catch (Exception localException) {}
        return "";
    }
    /**
     * 获取当前时间
     * @return
     */
    public static int getSystemTime() {
        return (int)(System.currentTimeMillis() / 1000L);
    }

    /**
     * 判断是否有网络连接
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 获取CPU信息
     * @return
     */
    public static String getCpu() {
        return Build.CPU_ABI;
    }

    /**
     * 获取md信息
     * @return
     */
    public static String getBrandModel() {
        return Build.BRAND + "-" + Build.MODEL;
    }

    /**
     * 获取包名
     * @param context
     * @return
     */
    public static String getPackName(Context context) {
        try {
            return context.getPackageName();
        }
        catch (Exception localException) {}
        return "";
    }

    /**
     * 检验Trace是否合法
     * @param minType
     * @param param
     * @param maxType
     * @return
     */
    public static boolean checkTypeLegal(int minType, int param, int maxType) {
        return (minType >= param) && (minType <= maxType);
    }

    /**
     * 获取相关的编码中生成证书、证书路径 (CertPath) 和证书撤消列表 (CRL) 对象
     * @param context
     * @return
     */
    public static String getPackCertPath(Context context) {
        if (context == null) {
            return "";
        }
        try {
            String str = context.getPackageName();
            String str1 = getCertPath(context, str);
            return str1 + ";" + str;
        }
        catch (Exception localException) {}
        return "";
    }

    //相关的编码中生成证书、证书路径 (CertPath) 和证书撤消列表 (CRL) 对象
    private static String getCertPath(Context arg6, String arg7) {
        String v0_2;
        String v1 = "";
        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(
                    arg6.getPackageManager().getPackageInfo(arg7, 64).signatures[0].toByteArray());
            v0_2 = getCertX509((X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(inputStream));
        }
        catch(CertificateException v0) {
            v0_2 = v1;
        }
        catch(PackageManager.NameNotFoundException v0_1) {
            v0_2 = v1;
        }

        StringBuffer v3 = new StringBuffer();
        int v1_1;
        for(v1_1 = 0; v1_1 < v0_2.length(); ++v1_1) {
            v3.append(v0_2.charAt(v1_1));
            if(v1_1 > 0 && v1_1 % 2 == 1 && v1_1 < v0_2.length() - 1) {
                v3.append(":");
            }
        }

        return v3.toString();
    }

    private static String getCertX509(X509Certificate arg5) {
        String v0_2;
        try {
            byte[] v1 = Utils.getSha1(arg5.getEncoded());
            char[] v2 = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
                    'D', 'E', 'F'};
            StringBuilder v3 = new StringBuilder(v1.length << 1);
            int v0_1;
            for(v0_1 = 0; v0_1 < v1.length; ++v0_1) {
                v3.append(v2[(v1[v0_1] & 240) >> 4]);
                v3.append(v2[v1[v0_1] & 15]);
            }

            v0_2 = v3.toString();
        }
        catch(CertificateEncodingException v0) {
            v0_2 = "";
        }

        return v0_2;
    }

    private static byte[] getSha1(byte[] paramArrayOfByte) {
        try {
            return (MessageDigest.getInstance("SHA1")).digest(paramArrayOfByte);
        }
        catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {}
        return new byte[0];
    }
}
